// src/store/features/user/userSlice.ts
import { createSlice } from "@reduxjs/toolkit";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";

const persistConfig = {
  key: "config",
  storage,
};

interface ConfigState {
  /** 是否为暗黑主题 */
  darkTheme: boolean;
  size: "" | "default" | "small" | "large";
  themeColor: string;
}

const initialState: ConfigState = {
  darkTheme: false,
  size: "",
  themeColor: "#409eff",
};

export const userSlice = createSlice({
  name: "config",
  initialState,
  reducers: {
    setDarkTheme(state, action) {
      state.darkTheme = action.payload;
      document.documentElement.classList.toggle("dark", action.payload);
    },
  },
});

// 导出同步 action creators
export const { setDarkTheme } = userSlice.actions;

export default persistReducer(persistConfig, userSlice.reducer);
